from django.db import models
from django.db.models import signals
from django.contrib.auth.management import create_superuser
from django.contrib.auth import models as auth_app

"""
Disable the prompt to create a superuser when the database is initialized
"""
signals.post_syncdb.disconnect(create_superuser, sender=auth_app, dispatch_uid = 'django.contrib.auth.management.create_superuser')


###############################################################################

class BaseModule(models.Model):
    description = models.CharField(max_length = 100, blank = True, null = True)
    created     = models.DateTimeField(auto_now_add = True)
    modified    = models.DateTimeField(auto_now = True)
    plugins     = models.XMLField(blank = True, null = True)

    class Meta:
        abstract = True



###############################################################################
class BaseOption(models.Model):
    """
    Generic Configuration option model
    """
    BASE_OPTION_FIELD_TYPES = (
        (u'STR', u'String'),
        (u'BOL', u'Boolean'),
        (u'INT', u'Integer'),
        (u'URL', u'URL'),
        (u'IP4', u'IP v4 Address'),
        (u'DAT', u'Date'),
        (u'XML', u'XML'),
    )
    BASE_OPTION_VALID_TYPES = (
        (u'LIST', u'Comma Delimited List'),
        (u'EXPR', u'Regular Expression'),
        (u'JSON', u'JSON Encoded List'),
    )

    name        = models.CharField(max_length = 50)
    description = models.CharField(max_length = 100)

    field_type  = models.CharField(max_length = 3, choices = BASE_OPTION_FIELD_TYPES)

    option_type  = models.CharField(max_length = 4, choices = BASE_OPTION_VALID_TYPES, blank = True, null = True)
    option       = models.CharField(max_length = 255, blank = True, null = True)

    created     = models.DateTimeField(auto_now_add = True)
    modified    = models.DateTimeField(auto_now = True)

    class Meta:
        abstract = True



###############################################################################

class Module(models.Model):
    """
    Module Management
    """
    name        = models.CharField(max_length = 50, unique = True)
    description = models.CharField(max_length = 100, blank = True, null = True)
    created     = models.DateTimeField(auto_now_add = True)
    modified    = models.DateTimeField(auto_now = True)

    enabled     = models.BooleanField(default = True)
    version     = models.CharField(max_length = 20)
    author      = models.CharField(max_length = 100)
    update_url  = models.URLField(verify_exists = False)

    # Custom configuration, stored in XML (why not?)
    config       = models.XMLField()

    # Inter-module dependancy tracking
    relationship = models.XMLField()

    def __unicode__(self):
        return self.name

    # So we can lookup modules by name (the natural key)
    def get_by_natural_key(self, name):
        return self.get(name = name)

    class Meta:
        ordering = ['name']


##########################################################
# These aren't really core...  Need to be somewhere else!
##########################################################
"""
#
# Network ranges
#
class Network(models.Model):
    name          = models.CharField(max_length = 50)
    description   = models.CharField(max_length = 100, blank = True, null = True)
    last_modified = models.DateTimeField(auto_now = True)

    enabled = models.BooleanField(default = True)
    network = models.IPAddressField()
    netmask = models.IPAddressField()

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']


#
# Generic Server
#
class Server(models.Model):
    hostname      = models.CharField(max_length = 255)
    ip_address    = models.IPAddressField(blank = True)
    description   = models.CharField(max_length = 100, blank = True, null = True)
    last_modified = models.DateTimeField(auto_now = True)
    enabled       = models.BooleanField(default = True)

    def __unicode__(self):
        return self.hostname

    class Meta:
        ordering = ['hostname']


class Voicemail(models.Model):
    user          = models.ForeignKey(User, blank = True, null = True)
    context       = models.ForeignKey(Context)
    mailbox       = models.CharField(max_length = 20)
    password      = models.CharField(max_length = 32, blank = True, null = True)
    enabled       = models.BooleanField(default = True)
    description   = models.CharField(max_length = 100, blank = True, null = True)
    last_modified = models.DateTimeField(auto_now = True)

    def __unicode__(self):
        return "%s@%s" % (self.mailbox, self.context)

    class Meta:
        ordering = ['mailbox']
        order_with_respect_to = 'context'
        unique_together = ('context', 'mailbox')
"""
